<? if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inventory extends CI_Controller {

	public function __construct()
	{
		parent::__construct();

		if(!$this->auth->is_admin() && !$this->auth->is_super_admin())
			$this->auth->verify();

		$this->load->model('item_model');
		$this->load->model('activities_model');
	}

	public function index()
	{
		$data['items'] = $this->item_model->get_items();
		$data['page'] = 'admin/inventory';
		$this->load->view('admin/index', $data);
	}

	public function add_edit_item($item_id=null)
	{
		if($_POST)
		{
			$item_data = array(
				'item_name' => $this->input->post('item_name'),
				'item_unit' => $this->input->post('item_unit'),
				'item_whole_sale_price' => $this->input->post('item_whole_sale_price'));
			if(!$item_id){
				$irp = (($this->input->post('item_whole_sale_price')*40) / 100) + $this->input->post('item_whole_sale_price');
				$item_data['item_retail_price'] = $irp;
				$item_data['item_quantity'] = $this->input->post('item_quantity');
			}else{
				$item_data['item_retail_price'] = $this->input->post('item_retail_price');
			}

			if($item_id) {
				$this->activities_model->add_activity(array('activity_content' => "<img src='html/img/app_48.png'  height='20px' width='20px' /> Updated <b><i>{$item_data['item_name']}</i></b>. <i>".$this->input->post('activity_comment')."</i>"));
				$this->item_model->update_item($item_data, $item_id);
			}else {
				$this->activities_model->add_activity(array('activity_content' => "<img src='html/img/inventory_add.png'  height='20px' width='20px' /> Added <b><i>{$item_data['item_name']}</i></b> in inventory, with {$item_data['item_quantity']} quantity. <i>".$this->input->post('activity_comment')."</i>"));
				$this->item_model->add_new_item($item_data);
			}
			exit();
		}
		$data['item'] = $this->item_model->get_item_by_id($item_id);
		$this->load->view('admin/dialog/add_edit_item', $data);
	}

	public function update_inv($id)
	{
		$data['item'] = $this->item_model->get_item_by_id($id);
		if($_POST)
		{
			$qty = $data['item']->item_quantity;
			$amount = $this->input->post('item_quantity');
			$option = $this->input->post('option');
			if($option == 'Checked In') {
				$this->activities_model->add_activity(array('activity_content' => "<img src='html/img/inventory_add.png'  height='20px' width='20px' /> Added {$amount} pcs. on <b><i>{$data['item']->item_name}</i></b>. <i>".$this->input->post('activity_comment')."</i>"));
				$this->item_model->update_item(array('item_quantity'=>$amount+$qty), $id);
			} else {
				$this->activities_model->add_activity(array('activity_content' => "<img src='html/img/inventory_minus.png' height='20px' width='20px' /> Removed {$amount} pcs. on <b><i>{$data['item']->item_name}</i></b>. <i>".$this->input->post('activity_comment')."</i>"));
				$this->item_model->update_item(array('item_quantity'=>$qty-$amount), $id);
			}
			exit();
		}
		$this->load->view('admin/dialog/inv_form', $data);
	}

	public function delete_item($item_id)
	{
		$item = $this->item_model->get_item_by_id($item_id);
		$this->activities_model->add_activity(array('activity_content' => "<img src='html/img/inventory_minus.png' height='20px' width='20px'/> Deleted <b><i>{$item->item_name}</i></b> in inventory."));
		$this->item_model->delete_item_by_id($item_id);
	}

	public function item_search($item_id)
	{
		$data['items'] = $this->item_model->search_items_by_id($item_id);
		$data['search'] = true;
		$this->load->view('admin/includes/_inventory_tbl', $data);
	}

	public function inv_tbl($idx=null)
	{
		$total_items = $this->item_model->get_items();
		$gt_wsp=0;$gt_rp=0;
		foreach($total_items->result() as $item){
			$gt_wsp=$gt_wsp+($item->item_whole_sale_price*$item->item_quantity);
			$gt_rp=$gt_rp+($item->item_retail_price*$item->item_quantity);
		}
		$data['gt_wsp'] = $gt_wsp;
		$data['gt_rp'] = $gt_rp;
		$data['items'] = $this->item_model->get_items_with_idx($idx);
		$data['search'] = false;
		$this->load->view('admin/includes/_inventory_tbl', $data);
	}

	// item autocomplete

	public function item_autocomplete()
	{
		$term = $this->input->post('term');
		$item = $this->item_model->search_by_item_name($term);
		$json = array();
		foreach($item->result() as $row) {
			$json[] = (object) array(
				'id' => $row->item_id,
				'value' => $row->item_name
			);
		}
		exit(json_encode($json));
	}
}